<?php
/**
 * Created by PhpStorm.
 * User: zhou
 * Date: 2017/2/13
 * Time: 14:52
 */
namespace Application\Controller;

use Application\Dao\AddressDao;
use Application\Service\AddressService;
use Application\Service\OrderService;
use Application\Service\PriceService;
use Application\Service\WaybillService;
use Application\Utils\OrderCreate;
use Infobird\Controller\BaseController;
use Infobird\Utils\GuidUtils;
use Infobird\Utils\HttpUtils;
use Infobird\Utils\LogUtils;

class OrderController extends BaseController
{
    public function indexAction(){
    	$yadress = isset($_REQUEST['yadress'])?$_REQUEST['yadress']:"";
    	$madress = isset($_REQUEST['madress'])?$_REQUEST['madress']:"";
    	return array(
    		"yadress"=>$yadress,
    		"madress"=>$madress,
    	);
    }

    public function waybillAction(){

    }

    public function oderSaveAction(){
        $rendName = isset($_REQUEST['rendName'])?$_REQUEST['rendName']:"";
        $rendTel = isset($_REQUEST['rendTel'])?$_REQUEST['rendTel']:"";
        $rendCity = isset($_REQUEST['rendCity'])?$_REQUEST['rendCity']:"";
        $rendAdress = isset($_REQUEST['rendAdress'])?$_REQUEST['rendAdress']:"";
        $orderWeight = isset($_REQUEST['orderWeight'])?$_REQUEST['orderWeight']:"";
        $textarea = isset($_REQUEST['textarea'])?$_REQUEST['textarea']:"";
        $signName = isset($_REQUEST['signName'])?$_REQUEST['signName']:"";
        $signTel = isset($_REQUEST['signTel'])?$_REQUEST['signTel']:"";
        $signCity = isset($_REQUEST['signCity'])?$_REQUEST['signCity']:"";
        $signAdress = isset($_REQUEST['signAdress'])?$_REQUEST['signAdress']:"";
        $goods = isset($_REQUEST['goods'])?$_REQUEST['goods']:"";
        $num = isset($_REQUEST['num'])?$_REQUEST['num']:"";
        $insert_dt = date("Y-m-d h:i:s");

        $giud = OrderCreate::CreateCartNo();
        $data = array(
            "sender_name"=>$rendName,
            "tel"=>$rendTel,
            "city"=>$rendCity,
            "adress"=>$rendAdress,
            "goods"=>$goods,
            "weight"=>$orderWeight,
            "num"=>$num,
            "remarks"=>$textarea,
            "insert_dt"=>$insert_dt,
            "order_no"=>$giud
        );

        $arr = array(
            "name"=>$signName,
            "tel"=>$signTel,
            "city"=>$signCity,
            "adress"=>$signAdress,
            "insert_dt"=>$insert_dt,
            "order_no"=>$giud
        );
        $logData = array(
            "loginfo"=>"正在入库",
            "arrive_dt"=>$insert_dt,
            "card_no"=>$giud,
             "insert_dt"=>$insert_dt,
        );

        $order = new OrderService();
        $adress = new AddressService();
        $log = new WaybillService();
        try{
            $order->insertSender($data);
            $adress->insertSender($arr);
            $log->insertData($logData);
            echo json_encode(array("result"=>"0","msg"=>"ok"));
            exit;
        }catch (\Exception $e){
            LogUtils::log("t_order.log", 'save'.$e->getMessage(),array("result"=>"2000"));
            exit;
        }
    }

    public function waybillInfoAction(){
        $orderNo = isset($_REQUEST['orderNo'])?$_REQUEST['orderNo']:"";
        $waybill = new WaybillService();
        try{
            $result = $waybill->loginfo($orderNo);
            echo json_encode($result);
            exit;
        }catch (\Exception $e){
            LogUtils::log("t_order.log", 'waybillInfo'.$e->getMessage(),array("result"=>"2000"));
            exit;
        }
    }

    public function serachPriceAction(){
        $oldadress = isset($_REQUEST['oldadress'])?$_REQUEST['oldadress']:"";
        $destination = isset($_REQUEST['destination'])?$_REQUEST['destination']:"";
//        $oldadress = "湖北";
//        $destination = "湖南";
        $price = new PriceService();
        try{
            $result = $price->serachPrice($oldadress,$destination);
            echo json_encode($result);
            exit;
        }catch (\Exception $e){
            LogUtils::log("t_order.log", 'serachPrice'.$e->getMessage(),array("result"=>"2000"));
            exit;
        }
    }
}